from celery import Celery
from study import settings
from django.core.mail import send_mail
import time
import os
import django

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shengxian.settings")
django.setup()


app = Celery("celery_tasks.tasks", broker="redis://127.0.0.1:6379/4")


@app.task
def send_active_email(email,username,token):
    subject = "学生管理系统欢迎你"  # 邮件标题
    message = ''  # 邮件正文
    sender = settings.EMAIL_FROM  # 发件人
    receiver = [email]  # 收件人
    html_message = """
             <h1>%s  恭喜您成为学生管理系统注册会员</h1><br/><h3>请您在1小时内点击以下链接进行账户激活</h3>
             <a href="http://127.0.0.1:8000/user/active/%s">http://127.0.0.1:8000/user/active/%s</a>
             """ % (username, token, token)
    send_mail(subject, message, sender, receiver, html_message=html_message)
    # 为了体现出celery异步完成发送邮件，这里睡眠
    time.sleep(5)

